/*
 * @Author: houmin 745762351@qq.com
 * @Date: 2023-11-29 10:06:07
 * @LastEditors: houmin 745762351@qq.com
 * @LastEditTime: 2023-12-03 23:50:52
 * @FilePath: \tch-h5\src\utils\time.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
export const isTimePassed = (targetTime) => {
  // 获取当前时间
  const currentTime = new Date();

  // 解析目标时间
  const [targetHour, targetMinute] = targetTime.split(':');

  // 设置目标时间
  const targetDateTime = new Date();
  targetDateTime.setHours(targetHour);
  targetDateTime.setMinutes(targetMinute);

  // 比较时间
  return currentTime.getTime() > targetDateTime.getTime();
}

export const formatTime = (timeStr, text = '') => {
  if (timeStr) {
    let formatTimeStr = timeStr.split('.')[0]
    const inputDate = new Date(formatTimeStr)
    const today = new Date()
    const tomorrow = new Date()
    tomorrow.setDate(today.getDate() + 1)

    if (inputDate.toDateString() === today.toDateString()) {
      const time = formatTimeStr.slice(10).replace(/\s/g, "")
      return {
        isExpiringSoon: true,
        name: `今天${time}到期`
      }
    } else if (inputDate.toDateString() === tomorrow.toDateString()) {
      const time = formatTimeStr.slice(10).replace(/\s/g, "")
      return {
        isExpiringSoon: true,
        name: `明天${time}到期`
      }
    } else {
      return {
        isExpiringSoon: false,
        name: `${text}：${formatTimeStr}`
      }
    }
  } else return '-'
}

export const getBusinessHours = (businessHours) => {
  const today = new Date()
  const todayDate = today.toISOString().split("T")[0]

  for (const schedule of businessHours) {
    const [startDate, endDate] = schedule.timeHour
    if (todayDate >= startDate && todayDate <= endDate) {
      return schedule.timeSlot[0]
    }
  }
  return "今日歇业"
}

// 示例
const businessHours = [
  {
    timeHour: ["2023-09-20", "2023-12-31"],
    timeSlot: ["09:00-17:00"],
  },
  {
    timeHour: ["2024-02-01", "2024-03-31"],
    timeSlot: ["09:00-17:00"],
  },
  {
    timeHour: ["2024-04-01", "2024-08-31"],
    timeSlot: ["09:00-18:00"],
  },
];

const todayHours = getBusinessHours(businessHours);
console.log(todayHours);


